<?php

$known_mime_types=array(
 	"pdf" => "application/pdf",
 	"txt" => "text/plain",
 	"html" => "text/html",
 	"htm" => "text/html",
	"exe" => "application/octet-stream",
	"zip" => "application/zip",
	"doc" => "application/msword",
	"xls" => "application/vnd.ms-excel",
	"ppt" => "application/vnd.ms-powerpoint",
	"gif" => "image/gif",
	"png" => "image/png",
	"jpeg"=> "image/jpg",
	"jpg" =>  "image/jpg",
	"php" => "text/plain"
 );
 
 if($mime_type==''){
	 $file_extension = strtolower(substr(strrchr($file,"."),1));
	 if(array_key_exists($file_extension, $known_mime_types)){
		$mime_type=$known_mime_types[$file_extension];
	 } else {
		$mime_type="application/force-download";
	 };
 };
	@ob_end_clean();
	header('Content-Type: text/plain' );
	
	$periode_id = isset($_GET['periode_id']) ? (int) $_GET['periode_id']: get_periode_gaji();
	$dperiode = my_get_data_by_id('pr_periode','periode_id',$periode_id);
	$filenamedt = date('Y-m',strtotime($dperiode['tanggal']));

	$komponen = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_id); 
	header('Content-Disposition: attachment; filename="slip_rincian_komponen_'.str_replace(" ","_",$komponen['nama_komponen_gaji']).$filenamedt.'_'.rand(1000,9999).'.txt"');
	header("Content-Transfer-Encoding: binary");
	header('Accept-Ranges: bytes'); 
	header("Cache-control: private");
	header('Pragma: private');
	header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");   

	$bulans = array(
		1=>'Januari',
		2=>'Februari',
		3=>'Maret',
		4=>'April',
		5=>'Mei',
		6=>'Juni',
		7=>'Juli',
		8=>'Agustus',
		9=>'September',
		10=>'Oktober',
		11=>'November',
		12=>'Desember',
	);
	$bln = (int)date('m' ,strtotime($dperiode['tanggal']));
	$namabulan = $bulans[$bln]; 
	$query = "SELECT a.karyawan_id  
		FROM pr_gaji_komponen a
		INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		WHERE a.periode_id={$periode_id} 
		AND a.komponen_id={$komponen_id} 
		AND (b.nik BETWEEN {$_GET['nik_start']} AND {$_GET['nik_end']} )
		ORDER BY b.karyawan_id  "
	;  
	$result = my_query($query);
	$total_record = my_num_rows($result);
	$total_page = ceil($total_record/40);
	$grand_nominal=0;  
	for($n=1;$n<=$total_page;$n++):
	$st = ($n - 1 )*40;
 ?>
 <?php echo str_pad('P.T.  K R A K A T A U  M E D I K A',65," ", STR_PAD_BOTH); ?> 
 <?php echo str_pad( 'DATA '.strtoupper($komponen['nama_komponen_gaji']),65," ", STR_PAD_BOTH); ?> 
 <?php echo str_pad('Periode  ( '. $namabulan.' '.date('Y').' )',65," ", STR_PAD_BOTH); ?> 
 <?php echo str_pad(date('d-m-Y H:i'),35).str_pad('Hal : '.$n,30," ", STR_PAD_LEFT); ?> 
 <?php echo str_repeat('_',66); ?> 
 <?php echo str_pad( 'NO',6," ",STR_PAD_BOTH). str_pad( 'N.I.K',9 ). str_pad( 'N A M A',30 ).str_pad('JUMLAH',20," ",STR_PAD_LEFT); ?> 
 <?php echo str_repeat('_',66); 
 $query2 = "SELECT a.karyawan_id,a.kalkulasi  
		FROM pr_gaji_komponen a
		INNER JOIN karyawan b ON a.karyawan_id = b.karyawan_id
		WHERE a.periode_id={$periode_id}
		AND a.komponen_id={$komponen_id} 
		AND (b.nik BETWEEN '{$_GET['nik_start']}' AND '{$_GET['nik_end']}' )
		ORDER BY b.karyawan_id LIMIT $st,40"
	; 
	$result2 = my_query($query2);
	$i=$st;
	$nominal_page =0;
	while($ey = my_fetch_array($result2)){
	$i++;
	$karyawan_temp=$ey['karyawan_id'];
	$karyawan = loaddata_karyawan($ey['karyawan_id']);
	$nominal_page += $ey['kalkulasi'];
	$grand_nominal +=$ey['kalkulasi'];
 ?> 
 <?php echo str_pad( $i.'  ',6," ",STR_PAD_LEFT). str_pad($karyawan['nik'] ,9 ). str_pad( $karyawan['nama'] ,30 ).str_pad(rp_format($ey['kalkulasi']),20," ",STR_PAD_LEFT); ?>  <?php if(($i%5) ==0 && ($i%40) !=0)echo "\n";
 }
 ?> 
 <?php echo str_repeat('_',66); ?> 
 <?php echo str_pad( 'Total',6," ",STR_PAD_BOTH). str_pad( ' ',9 ). str_pad( ' ',30 ).str_pad(rp_format($nominal_page) ,20," ",STR_PAD_LEFT); ?> 
 <?php echo str_repeat('_',66); ?> 
 <?php echo str_pad( ' Grand Total',15 ) . str_pad( ' ',30 ).str_pad(rp_format($grand_nominal) ,20," ",STR_PAD_LEFT); ?> 
 <?php echo str_repeat('_',66); ?>
 
 <?php if( $n == $total_page){  echo "\n\n "; 
  $bl = (int)date('m'); echo str_pad( str_pad('Cilegon, '.date('d').' '.$bulans[$bl].' '.date('Y').'     '  , 25, " ", STR_PAD_BOTH ),65," ",STR_PAD_LEFT); 
   echo "\n\n\n\n\n\n";
 
 echo str_pad( '('.str_pad( MAN_DEPARTEMEN_HEAD_OFFICE_NAME ,25," ",STR_PAD_BOTH).')  ',65," ",STR_PAD_LEFT);  
echo "\n";
 echo str_pad( ' '.str_pad( MAN_DEPARTEMEN_HEAD_OFFICE ,25," ",STR_PAD_BOTH).'   ',65," ",STR_PAD_LEFT); 
 } ?>
 
 <?php  echo  chr(12); 
 endfor; 
 ?>
 